#import "ALElement.h"
#import "ALElementTemplate.h"
#import "ALEntryTemplate.h"

@implementation ALElement

@dynamic attachmentImage;
@synthesize isPrimary;

+(NSSet *)keyPathsForValuesAffectingIsPrimary {
    return [NSSet setWithObject:@"template.primaryElementEntry"];
}

-(BOOL)isPrimary {
    return [[self template] primaryElementEntry] != nil;
}

-(NSURL *)attachmentURL {
	if (![self attachmentURLString])
		return nil;
	return [NSURL URLWithString:[self attachmentURLString]];
}

-(void)setAttachmentURL:(NSURL *)aURL {
	[self setAttachmentURLString:[aURL absoluteString]];
}

+(NSString *)keyForElementType:(ALElementType)theType {
	switch (theType) {
		case ALElementTypeBool:
			return @"booleanValue";
		case ALElementTypeAttachment:
			return @"attachmentURLString";
		case ALElementTypeDate:
			return @"dateValue";
		case ALElementTypeRating:
			return @"ratingValue";
		case ALElementTypeText:
			return @"textValue";
		default:
			break;
	}
	return nil;
}

-(NSString *)elementValueKey {
	return [ALElement keyForElementType:[[self elementType] integerValue]];
}

-(id)elementValue {
	return [self valueForKeyPath:[self elementValueKey]];
}

-(void)setElementValue:(id)elementObject {
	[self setValue:elementObject forKey:[self elementValueKey]];
}

+(NSSet *)keyPathsForValuesAffectingLabel {
	return [NSSet setWithObject:@"template.label"];
}

-(NSString *)label {
	return [[self template] label];
}

-(void)setLabel:(NSString *)label {
	[[self template] setLabel:label];
}

+(NSSet *)keyPathsForValuesAffectingElementType {
	return [NSSet setWithObject:@"template.elementType"];
}

-(NSNumber *)elementType {
	return [[self template] elementType];
}

-(void)setElementType:(NSNumber *)elementType {
	[[self template] setElementType:elementType];
}

+(NSSet *)keyPathsForValuesAffectingLineNumber {
	return [NSSet setWithObject:@"template.lineNumber"];
}

-(NSNumber *)lineNumber {
	return [[self template] lineNumber];
}

-(void)setLineNumber:(NSNumber *)lineNumber {
	[[self template] setLineNumber:lineNumber];
}

+(NSSet *)keyPathsForValuesAffectingInfoPListKey {
	return [NSSet setWithObject:@"template.infoPListKey"];
}

-(NSString *)infoPListKey {
	return [[self template] infoPListKey];
}

-(void)setInfoPListKey:(NSString *)infoPListKey {
	[[self template] setInfoPListKey:infoPListKey];
}

@end
